$(function () {

    $.get("/commodity/getHotWord",data => {
        $.each(data,function (index,elements) {
            if(index > 5)
                return;
            $("<span></span>").html(elements).appendTo(".hot_word");
        })

    });

    searchKeyword($("#qq"),$("#q"));
    searchKeyword($(".search_min button"),$(".search_min input"));

   tip($("#q"),$(".search_info"),$("#qq"));
   tip($(".search_min>input"),$(".top_search_info"),$(".search_min button"));






    $.get("/car/car_count",function (data) {
        if(data.carcount != 0){
            $(".car_num>span").html(data.carcount);
        }
    });

    var $index_left =$(".index_left");
    var leftTop = $index_left.height();
    // commodity_type
    $.post("/commodity/commodity_type",{type:"first"},function (data) {
         $.each(data,function (index,element) {
             $("<li></li>").text(element).appendTo(".commodity ul");
         });
        leftTop = $(".index_left").height();
     },"json");
    // 子类别

    $index_left.on("mouseenter",".commodity li",function () {
        var $typeInfo = $(".type_info ul");
        $typeInfo.empty();
        var $this = $(this);
        $this.addClass(".commodity_a");
        var $type_info = $(".type_info");
        var num = $this.prevAll().length;
        var tops = num * 100 + 200;
        console.log($type_info.css("top",tops+"px"));
        $type_info.stop(true,true).animate({width:"484px"},1000).delay(1000);
        //post
        $.post("/commodity/commodity_type",{type:$this.html()},function (data) {
            if(data.length == 0){
                $("<li><a></a></li>").find("a").attr("href","javascript:;").html("该分类还未有子分类").end().appendTo($typeInfo);
            }else{
                $.each(data,function (index,element) {
                    var url = '/commodity/list?type=' +element[0];
                    $("<li><a></a></li>").find("a").attr("href",url).html(element[1]).end().appendTo($typeInfo);
                })
            }
        },"json");
        console.log($(this).html());
    });
    $index_left.on("mouseleave",".left_info",function () {
        $(".type_info").stop(true,true).animate({width:"0px"},1000).delay(1000);
    });
    // to top
    $(".toTop").on("click",function () {
        $('html,body').stop().animate({scrollTop:0});
    });

    // 自动收缩
    $index_left.mouseenter(function () {
        if($(this).height() <= 80){
            $(".index_left").css("overflow","visible");
            $index_left.stop().animate({height:leftTop},2000).find(".wenzhi").html("门店用户").end();
        }
    });

    $("#user_pack_up").click(function () {
        $(".index_left").css("overflow","hidden");
        $index_left.stop().animate({height:80},2000).find(".wenzhi").html("更多信息");
    });
    // 滑动的top栏
    $(window).on("scroll",function () {
        //console.log(parseInt($(window).scrollTop()));
        if(parseInt($(".bottom").offset().top) + parseInt($(".bottom").height()) <= parseInt($(window).scrollTop())){
            $(".top_nav").fadeIn();
        }else{
            $(".top_nav").fadeOut();
        }
    });
    // 给用户注销
    $("#log_out").on("click",function () {
        $.ajax({
            async : false,
            type: "POST",
            url : "/users/log_out",
            success : function (data) {
                if(data.state =="ok"){
                    layer.msg("用户注销成功", {
                        time: 1000, //2s后自动关闭
                    },function () {
                        window.location.reload();
                    });
                }else{
                    layer.msg("未登录,请您先登录", {
                        time: 2000, //2s后自动关闭
                    },function () {
                        window.location.reload();
                    });
                }
            },
            dataType : "JSON"
        });
    });


    /* 轮播图 */
    lunbo();
    $(".hot_good").mouseenter(function () {
        $(this).find(".hot_info").stop().slideDown(500);

    });
    $(".hot_good").mouseleave(function () {
        $(this).find(".hot_info").stop().slideUp(500);
    });
    // 各种条件查询
    $("#price").on("click",function () {
        if(window.state){
            window.state = false;
            conditionLoadIndex($(this),$(".good_list"),"pasc","pdesc");
            if($("#price").attr("asc") != undefined){
                $("#price_w").html("&#xea3a;");
            }else{
                $("#price_w").html("&#xea3e;");
            }
        }else{
            layer.msg("请等待数据加载...", {
                time: 2000, //20s后自动关闭
            });
        }
    });
    $("#sales").on("click",function () {
        if(window.state){
            window.state = false;
            conditionLoadIndex($(this),$(".good_list"),"sasc","sdesc");
            if($(this).attr("asc") != undefined){
                $("#sales_w").html("&#xea3a;");
            }else{
                $("#sales_w").html("&#xea3e;");
            }
        }else{
            layer.msg("请等待数据加载...", {
                time: 2000, //20s后自动关闭
            });
        }

    });

});

function lunbo(){
    var $top = $(".top");
    play();
    f =true;
    $top.mouseenter(function () {
        $(".right_btn,.left_btn").stop().fadeIn(500);
        clearInterval(window.timeId);
    });
    $top.mouseleave(function () {
        f = false;
        $(".right_btn,.left_btn").stop().fadeOut(500);
        play();
    });
    $(".left_btn").on("click",function () {
        var $ul = $(".top ul");
        var $li = $(".top li");
        var index = parseInt($ul.attr("currentIndex"));
        index --;
        if(index <= 0){
            index = $li.length - 1;
        }
        $li.eq(index).fadeIn(1000).siblings().fadeOut(1000);
        $ul.attr("currentIndex",index);
    });
    $(".right_btn").on("click",function () {
        var $ul = $(".top ul");
        var $li = $(".top li");
        var index = parseInt($ul.attr("currentIndex"));
        index ++;
        if(index >= $li.length){
            index = 0;
        }
        $li.eq(index).fadeIn(1000).siblings().fadeOut(1000);
        $ul.attr("currentIndex",index);
    });
}
function play() {
    window.timeId = setInterval(function () {
        var $ul = $(".top ul");
        var $li = $(".top li");
        var index = parseInt($ul.attr("currentIndex"));
        if(index == $li.length -1 ){
            index = 0;
        }
        $li.eq(index).fadeIn(1000).siblings().fadeOut(1000);
        $ul.attr("currentIndex",++index);
    },5000);

}